using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SavedVariablesParser.Line {
    /// <summary>
    /// Represents a line in a World of Warcraft saved variables file that matches the beginning of array format.
    /// </summary>
    public class BeginArray : LineBase{
        /// <summary>
        /// 	<para>Initializes an instance of the <see cref="BeginArray"/> class.</para>
        /// </summary>
        /// <param name="text">The text of the line.</param>
        /// <param name="type">Specifies the type of line.</param>
        /// <param name="lineNumber">Specifies the line number within the original file where this line can be found.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para>The argument <paramref name="text"/> is <langword name="null"/>.</para>
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// 	<para>The argument <paramref name="text"/> is out of range.</para>
        /// 	<para>-or-</para>
        /// 	<para>The argument <paramref name="lineNumber"/> is out of range.</para>
        /// </exception>
        public BeginArray(string text, LineType type, int lineNumber) {
            if (text == null) {
                throw new ArgumentNullException("text");
            }
            if (text.Length == 0) {
                throw new ArgumentOutOfRangeException("text", "The value of 'text' cannot be a zero length string.");
            }
            if (lineNumber <= 0) {
                throw new ArgumentOutOfRangeException("lineNumber", "The value of 'lineNumber' must be greater than zero.");
            }

            base.text = text;
            base.type = type;
            base.lineNumber = lineNumber;
        }

        /// <summary>
        /// Renders the line and returns a string representing the rendered value.
        /// </summary>
        /// <returns>The text line rendered in a predefined format.</returns>
        public override string Render() {
            StringBuilder builder = new StringBuilder();
            this.GenerateBeginningArrayNode(this.text, builder);
            return builder.ToString();
        }

        /// <summary>
        /// Renders the line to the supplied <see cref="StringBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to render the string to.</param>
        public override void Render(StringBuilder builder) {
            this.GenerateBeginningArrayNode(this.text, builder);
        }

        /// <summary>
        /// Creates the beginning array node tag.
        /// </summary>
        /// <param name="line">The line used as the source of the array node.</param>
        /// <param name="builder">The <see cref="StringBuilder"/> to render the tag to.</param>
        private void GenerateBeginningArrayNode(string line, StringBuilder builder) {
            //<array name="AuctionConfig">
            builder.Append("<array name=");
            builder.Append("\"");
            builder.Append(this.GetArrayNodeName(line));
            builder.Append("\">");
            builder.Append(Environment.NewLine);
        }

        /// <summary>
        /// Gets the name of the array node for rendering.
        /// </summary>
        /// <param name="line">The line of text to parse to find the name of the array node.</param>
        /// <returns>The name of the array node.</returns>
        private string GetArrayNodeName(string line) {
            line = this.PurgeTabs(line);
            string key = line.Substring(0, line.LastIndexOf("=") - 1).Replace("\"", "");
            return XmlConvert.EncodeName(key);
        }
    }
}
